package com.ruoyi.supplier.controller;

import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.supplier.domain.TbCheckProductDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbCheckProduct;
import com.ruoyi.supplier.service.ITbCheckProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 验货Controller
 *
 * @author ruoyi
 * @date 2021-07-02
 */
@RestController
@RequestMapping("/supplier/checkProduct")
public class TbCheckProductController extends BaseController {

    public static final Logger logger= LoggerFactory.getLogger(TbCheckProductController.class);

    @Autowired
    private ITbCheckProductService tbCheckProductService;

    /**
     * 查询验货列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbCheckProduct tbCheckProduct) {
        startPage();
        List<TbCheckProduct> list = tbCheckProductService.selectTbCheckProductList(tbCheckProduct);
        return getDataTable(list);
    }

    /**
     * 导出验货列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:export')")
    @Log(title = "验货", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbCheckProduct tbCheckProduct) {
        List<TbCheckProduct> list = tbCheckProductService.selectTbCheckProductList(tbCheckProduct);
        ExcelUtil<TbCheckProduct> util = new ExcelUtil<TbCheckProduct>(TbCheckProduct.class);
        return util.exportExcel(list, "checkProduct");
    }

    /**
     * 获取验货详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:query')")
    @GetMapping(value = "/{checkIdPk}")
    public AjaxResult getInfo(@PathVariable("checkIdPk") Long checkIdPk) {
        return AjaxResult.success(tbCheckProductService.selectTbCheckProductById(checkIdPk));
    }

    /**
     * 新增验货
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:add')")
    @Log(title = "验货", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCheckProduct tbCheckProduct) {
        return toAjax(tbCheckProductService.insertTbCheckProduct(tbCheckProduct));
    }

    /**
     * 修改验货
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:edit')")
    @Log(title = "验货", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCheckProduct tbCheckProduct) {
        return toAjax(tbCheckProductService.updateTbCheckProduct(tbCheckProduct));
    }

    /**
     * 删除验货
     */
    @PreAuthorize("@ss.hasPermi('supplier:checkProduct:remove')")
    @Log(title = "验货", businessType = BusinessType.DELETE)
    @DeleteMapping("/{checkIdPks}")
    public AjaxResult remove(@PathVariable Long[] checkIdPks) {
        int i = tbCheckProductService.deleteTbCheckProductByIds(checkIdPks);
        if (i == -1) {
            return new AjaxResult(HttpStatus.SUCCESS, "包含已提交数据，不能删除", -1);
        }
        return toAjax(i);
    }


    /**
     * 检查条形码是否重复
     */
    @PostMapping("/checkBarcode")
    public AjaxResult checkBarcode(@RequestBody TbCheckProductDetail tbCheckProductDetail) {
        int i = tbCheckProductService.checkBarcode(tbCheckProductDetail);
        if(i>0){
            return new AjaxResult(HttpStatus.ERROR, tbCheckProductDetail.getBarcode()+" 条形码重复", -1);
        }
        return new AjaxResult(HttpStatus.SUCCESS, tbCheckProductDetail.getBarcode()+" 条形码可以使用");
    }
}
